package com.library.management.service.impl;

import java.util.List;

import com.library.common.core.domain.model.LoginUser;
import com.library.common.utils.DateUtils;
import com.library.common.utils.SecurityUtils;
import com.library.management.domain.LibraryBook;
import com.library.management.domain.vo.LibraryBorrowVo;
import com.library.management.service.ILibraryBookService;
import com.library.management.service.ILibraryBorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.library.management.mapper.LibraryBorrowMapper;
import com.library.management.domain.LibraryBorrow;

/**
 * 借阅记录Service业务层处理
 * 
 * @author library
 * @date 2023-03-26
 */
@Service
public class LibraryBorrowServiceImpl implements ILibraryBorrowService
{
    @Autowired
    private LibraryBorrowMapper libraryBorrowMapper;
    @Autowired
    private ILibraryBookService libraryBookService;

    /**
     * 查询借阅记录
     * 
     * @param id 借阅记录主键
     * @return 借阅记录
     */
    @Override
    public LibraryBorrow selectLibraryBorrowById(Long id)
    {
        return libraryBorrowMapper.selectLibraryBorrowById(id);
    }

    /**
     * 查询借阅记录列表
     * 
     * @param libraryBorrow 借阅记录
     * @return 借阅记录
     */
    @Override
    public List<LibraryBorrowVo> selectLibraryBorrowList(LibraryBorrow libraryBorrow)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        libraryBorrow.setCreateBy(String.valueOf(loginUser.getUserId()));
        return libraryBorrowMapper.selectLibraryBorrowList(libraryBorrow);
    }
    @Override
    public List<LibraryBorrowVo> selectLibraryBorrowList1(LibraryBorrow libraryBorrow)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        libraryBorrow.setReaderId(loginUser.getUserId());
        libraryBorrow.setReturnTime(null);
        return libraryBorrowMapper.selectLibraryBorrowList1(libraryBorrow);
    }

    /**
     * 新增借阅记录
     * 
     * @param libraryBorrow 借阅记录
     * @return 结果
     */
    @Override
    public int insertLibraryBorrow(LibraryBorrow libraryBorrow)
    {
        libraryBorrow.setCreateTime(DateUtils.getNowDate());
        return libraryBorrowMapper.insertLibraryBorrow(libraryBorrow);
    }

    /**
     * 修改借阅记录
     * 
     * @param libraryBorrow 借阅记录
     * @return 结果
     */
    @Override
    public int updateLibraryBorrow(LibraryBorrow libraryBorrow)
    {
        libraryBorrow.setUpdateTime(DateUtils.getNowDate());
        return libraryBorrowMapper.updateLibraryBorrow(libraryBorrow);
    }

    /**
     * 批量删除借阅记录
     * 
     * @param ids 需要删除的借阅记录主键
     * @return 结果
     */
    @Override
    public int deleteLibraryBorrowByIds(Long[] ids)
    {
        return libraryBorrowMapper.deleteLibraryBorrowByIds(ids);
    }

    /**
     * 删除借阅记录信息
     * 
     * @param id 借阅记录主键
     * @return 结果
     */
    @Override
    public int deleteLibraryBorrowById(Long id)
    {
        return libraryBorrowMapper.deleteLibraryBorrowById(id);
    }

    @Override
    public int returnBooks(String[] ids) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<LibraryBorrow> list=selectLibraryBorrowByIds(ids);
        list.forEach(borrow->{
            //恢复图书存储存储量
            LibraryBook book = libraryBookService.selectLibraryBookById(borrow.getBookId());
            book.setBookLend(book.getBookLend()-1);
            libraryBookService.updateLibraryBook(book);
            //更新还书时间
            borrow.setReturnTime(DateUtils.getNowDate());
            borrow.setUpdateBy(String.valueOf(loginUser.getUserId()));
            borrow.setUpdateTime(DateUtils.getNowDate());
            updateLibraryBorrow(borrow);
        });
        return 1;
    }

    @Override
    public int renew(String[] ids) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<LibraryBorrow> list=selectLibraryBorrowByIds(ids);
        list.forEach(borrow->{
            //更新借书时间
            borrow.setBorrowTime(DateUtils.getNowDate());
            borrow.setUpdateBy(String.valueOf(loginUser.getUserId()));
            borrow.setUpdateTime(DateUtils.getNowDate());
            updateLibraryBorrow(borrow);
        });
        return 1;
    }

    private List<LibraryBorrow> selectLibraryBorrowByIds(String[] ids) {
        return libraryBorrowMapper.selectLibraryBorrowByIds(ids);
    }
}
